//
//  SearchView.m
//  CarsFiles_QYB
//
//  Created by 程虎 on 2017/3/18.
//  Copyright © 2017年 程虎. All rights reserved.
//

#import "SearchView.h"
#import "LocationTools.h"
#define kMaxLength 18

@interface SearchView()<UITextFieldDelegate>
{
    NSString *searchString;
}
@property (nonatomic, assign) NSInteger types;
@property (nonatomic, strong) UIButton *searchBtn;
@property (nonatomic, strong) UITextField *searchField;
@property (nonatomic, strong) UILabel *locationLab;
@property (nonatomic, strong) LocationTools *locationTools;
@property (nonatomic, strong) UIViewController *showVC;
@end

@implementation SearchView

- (instancetype)initWithFrame:(CGRect)frame Type:(NSInteger )types ShowView:(UIViewController *)showVC
{
    self = [super initWithFrame:frame];
    if (self) {
        self.showVC = showVC;
        self.types = types;
        
        [self config_UI];
//        [[PermissionManager getPermissionManager] requestLocationPermissionBlock:^(BOOL b) {
//            
//        }];
//        self.locationTools = [[LocationTools alloc] init];
//        [self.locationTools startLocation];
//        WeakSelf(weakSelf);
//        self.locationTools.locationBlock = ^(LocationModel *model) {
//            if (model != nil) {
//                weakSelf.locationLab.text = model.City;
//                [Globle getInstance].locations = model;
//            }
//        };
    }
    return self;
}

- (void)setLocationStr:(NSString *)locationStr
{
    _locationStr = locationStr;
    self.locationLab.text = _locationStr;
}

- (void)setPlaceholderStr:(NSString *)placeholderStr
{
    _placeholderStr = placeholderStr;
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; // 创建属性字典
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:14]; // 设置font
    attrs[NSForegroundColorAttributeName] = [UIColor whiteColor]; // 设置颜色
    NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:self.placeholderStr attributes:attrs]; // 初始化富文本占位字符串
    _searchField.attributedPlaceholder = attStr;
}

- (void)config_UI
{
    UIImageView *bkimg = [UIImageView new];
    bkimg.userInteractionEnabled = YES;
    bkimg.image = [Util stretchImage:[UIImage imageNamed:self.types == 0 ? @"bk_top_home" : @"BG_nav"] top:0.1 right:0.1 bottom:0.9 left:0.1 resizingMode:UIImageResizingModeStretch];
    
    UIView *leftView = [self creatLeftView];
    
    UIView *rightView = [self creatRightView];
    rightView.hidden = self.types == 0 ? YES : NO;
  
    [self addSubview:bkimg];
    [bkimg sd_addSubviews:@[leftView,rightView]];

    CGFloat left = 18;
    
    bkimg.sd_layout
    .spaceToSuperView(UIEdgeInsetsZero);
    
    leftView.sd_layout
    .leftSpaceToView(bkimg, 0)
    .bottomSpaceToView(bkimg, 15)
    .heightIs(18.5);
    
    rightView.sd_layout
    .leftSpaceToView(leftView,left)
    .centerYEqualToView(leftView)
    .rightSpaceToView(bkimg,10.5)
    .heightIs(30);
}

#pragma mark - 创建View
- (UIView *)creatLeftView
{
    UIView *views = [UIView new];
    views.backgroundColor = [UIColor clearColor];
    
    UIImageView *imgs = [UIImageView new];
    imgs.image = [UIImage imageNamed:@"location_img"];

    self.locationLab = [[Tools shareInstance] creat_Label:[Globle getInstance].locations.City.length > 0 ? [Globle getInstance].locations.City : @"定位中..." Color:[UIColor whiteColor] Font:18 tag:-1 Alignment:1 Lines:1];
    
    self.locationLab.backgroundColor = [UIColor clearColor];

    [views sd_addSubviews:@[imgs, self.locationLab]];
    
    CGFloat left = 16;
    
    imgs.sd_layout
    .leftSpaceToView(views, left)
    .centerYEqualToView(views)
    .widthIs(15)
    .heightIs(18.5);
    
    self.locationLab.sd_layout
    .leftSpaceToView(imgs, 6.5)
    .centerYEqualToView(imgs)
    .heightIs(14);
    
    [self.locationLab setSingleLineAutoResizeWithMaxWidth:100];
    [views setupAutoWidthWithRightView:self.locationLab rightMargin:10];
    
    return views;
}

- (UIView *)creatRightView
{
    UIView *views = [UIView new];
    views.backgroundColor = [UIColor clearColor];
    LRViewRadius(views, 15, 0.5, [UIColor clearColor]);
    
    UIImageView *bkimg = [UIImageView new];
    bkimg.userInteractionEnabled = YES;
    bkimg.image = [UIImage imageNamed:self.types == 0 ? @"searchBK_b" : @"searchBK_w"];
    
    UIImageView *search_img = [UIImageView new];
    search_img.image = [UIImage imageNamed:@"search_view"];
    
    _searchField = [UITextField new];
    _searchField.delegate = self;
    _searchField.font = LRFont(14);
    _searchField.textColor = [UIColor whiteColor];
    _searchField.placeholder = self.placeholderStr;
    _searchField.borderStyle = UITextBorderStyleNone;
    _searchField.returnKeyType = UIReturnKeySearch;
    _searchField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _searchField.backgroundColor = [UIColor clearColor];
    
    [views addSubview:bkimg];
    [bkimg sd_addSubviews:@[_searchField,search_img]];
    
    bkimg.sd_layout
    .spaceToSuperView(UIEdgeInsetsZero);
    
    search_img.sd_layout
    .leftSpaceToView(bkimg,15)
    .centerYEqualToView(bkimg)
    .widthIs(18)
    .heightIs(16);
    
    _searchField.sd_layout
    .leftSpaceToView(search_img,5)
    .topSpaceToView(bkimg,0)
    .rightSpaceToView(bkimg,15)
    .bottomSpaceToView(bkimg,0);
    
    return views;
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
//    CXSearchController *searchVC = [[CXSearchController alloc] init];
//    searchVC.placeStr = @"搜索村落";
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchVC];
//    [self.showVC presentViewController:nav animated:YES completion:nil];
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (self.searchBlock) {
        self.searchBlock(textField.text);
    }
    return YES;
}

@end
